<template>
  <div class="recharge-page">
    <h2 class="recharge-title">账户充值</h2>

    <!-- 选择充值金额 -->
    <el-radio-group v-model="radio" @change="handleRadioChange" class="recharge-radio-group">
      <el-radio label="50" class="recharge-radio">
        <span class="recharge-label">充值50元</span>
        <span class="bonus-label">（赠送5元）</span>
      </el-radio>
      <el-radio label="100" class="recharge-radio">
        <span class="recharge-label">充值100元</span>
        <span class="bonus-label">（赠送15元）</span>
      </el-radio>
      <el-radio label="200" class="recharge-radio">
        <span class="recharge-label">充值200元</span>
        <span class="bonus-label">（赠送35元）</span>
      </el-radio>
      <el-radio label="300" class="recharge-radio">
        <span class="recharge-label">充值300元</span>
        <span class="bonus-label">（赠送55元）</span>
      </el-radio>
      <el-radio label="custom" class="recharge-radio">自定义金额</el-radio>
    </el-radio-group>

    <!-- 自定义金额输入框 -->
    <el-form-item label="自定义金额" v-if="isCustomAmountVisible" class="custom-amount-item">
      <el-input
        v-model="customAmount"
        placeholder="请输入自定义金额"
        type="number"
        class="custom-amount-input">
        <template #prefix>
          <span class="amount-prefix">¥</span>
        </template>
      </el-input>
    </el-form-item>

    <!-- 支付方式选择 -->
    <div class="payment-container" v-if="radio">
      <p class="payment-title">请选择支付方式</p>
      <div class="payment-methods">
        <el-radio-group v-model="paymentMethod" @change="handlePaymentMethodChange">
          <el-radio label="wechat" class="payment-option">
            <img src="@/assets/images/wechat-pay.jpg" alt="微信支付" class="payment-icon">
            <span>微信支付</span>
          </el-radio>
          <el-radio label="alipay" class="payment-option">
            <img src="@/assets/images/alipay.png" alt="支付宝" class="payment-icon">
            <span>支付宝</span>
          </el-radio>
        </el-radio-group>
      </div>
    </div>

    <!-- 提交按钮 -->
    <div class="button-container">
      <el-button type="primary" @click="showConfirmDialog" class="submit-button" :disabled="!paymentMethod">
        确认充值
      </el-button>
    </div>

    <!-- 确认充值弹窗 -->
    <el-dialog
      v-model="dialogVisible"
      title="确认充值"
      width="400px"
      center
      class="recharge-dialog"
    >
      <div class="dialog-content">
        <div class="amount-info">
          <span class="amount-label">充值金额：</span>
          <span class="amount-value">¥{{ isCustomAmountVisible ? customAmount : radio }}</span>
        </div>
        <div class="bonus-info" v-if="combo.bonusMoney && !isCustomAmountVisible">
          <span class="bonus-text">赠送金额：</span>
          <span class="bonus-value">¥{{ combo.bonusMoney }}</span>
        </div>
        <div class="payment-info">
          <span class="payment-label">支付方式：</span>
          <span class="payment-value">{{ paymentMethod === 'wechat' ? '微信支付' : '支付宝' }}</span>
        </div>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitRecharge">
            确认支付
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { ElMessage } from 'element-plus';
import { userRecharge, userCustomRecharge } from '@/api/combo/combo';

// 数据模型
const radio = ref('50'); // 默认选中50元
const customAmount = ref(''); // 自定义金额
const isCustomAmountVisible = ref(false); // 控制自定义金额框的显示
const combo = ref({
  comboId: '',
  comboMoney: '',
  bonusMoney: '',
  comboDetails: ''
});

// 新增数据
const dialogVisible = ref(false);
const paymentMethod = ref('');

// 处理选择变化
function handleRadioChange() {
  if (radio.value === '50') {
    combo.value = {
      comboId: '1',
      comboMoney: '50',
      bonusMoney: '5',
      comboDetails: '充值50元（赠送5元）'
    };
    isCustomAmountVisible.value = false; // 隐藏自定义金额输入框
  } else if (radio.value === '100') {
    combo.value = {
      comboId: '2',
      comboMoney: '100',
      bonusMoney: '15',
      comboDetails: '充值100元（赠送15元）'
    };
    isCustomAmountVisible.value = false; // 隐藏自定义金额输入框
  } else if (radio.value === '200') {
    combo.value = {
      comboId: '3',
      comboMoney: '200',
      bonusMoney: '35',
      comboDetails: '充值200元（赠送35元）'
    };
    isCustomAmountVisible.value = false; // 隐藏自定义金额输入框
  } else if (radio.value === '300') {
    combo.value = {
      comboId: '4',
      comboMoney: '300',
      bonusMoney: '55',
      comboDetails: '充值300元（赠送55元）'
    };
    isCustomAmountVisible.value = false; // 隐藏自定义金额输入框
  } else if (radio.value === 'custom') {
    combo.value = {
      comboId: '',
      comboMoney: '',
      bonusMoney: '',
      comboDetails: ''
    };
    isCustomAmountVisible.value = true; // 显示自定义金额输入框
  }
}

// 处理支付方式变化
function handlePaymentMethodChange() {
  // 不需要额外处理，因为支付方式变化会自动触发
}

// 显示确认弹窗
function showConfirmDialog() {
  if (isCustomAmountVisible.value && (!customAmount.value || customAmount.value <= 0)) {
    ElMessage.warning('请输入有效的充值金额');
    return;
  }
  dialogVisible.value = true;
}

// 修改提交充值函数
function submitRecharge() {
  const params = {
    ...combo.value,
    paymentMethod: paymentMethod.value
  };

  if (isCustomAmountVisible.value) {
    params.comboMoney = customAmount.value;
    userCustomRecharge(params).then(res => {
      if (res.code === 200) {
        ElMessage.success(res.msg);
        dialogVisible.value = false;
      } else {
        ElMessage.error(res.msg);
      }
    });
  } else {
    userRecharge(params).then(res => {
      if (res.code === 200) {
        ElMessage.success(res.msg);
        dialogVisible.value = false;
      } else {
        ElMessage.error(res.msg);
      }
    });
  }
}
</script>

<style scoped>
.recharge-page {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 40px;
  background: linear-gradient(145deg, #ffffff, #f5f7fa);
  min-height: 500px;
  border-radius: 20px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
}

.recharge-title {
  color: #2c3e50;
  font-size: 28px;
  font-weight: 600;
  margin-bottom: 30px;
  text-align: center;
  position: relative;
}

.recharge-title::after {
  content: '';
  position: absolute;
  bottom: -10px;
  left: 50%;
  transform: translateX(-50%);
  width: 50px;
  height: 3px;
  background: linear-gradient(to right, #409eff, #67c2ff);
  border-radius: 3px;
}

.recharge-radio-group {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 25px;
  width: 100%;
  max-width: 1000px;
  margin-top: 30px;
}

.recharge-radio {
  padding: 30px;
  border-radius: 18px;
  background: #ffffff;
  border: 2px solid #e6e8eb;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative;
  overflow: hidden;
  min-height: 120px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.recharge-radio:hover {
  border-color: #409eff;
  transform: translateY(-2px);
  box-shadow: 0 8px 20px rgba(64, 158, 255, 0.15);
}

.recharge-radio.is-checked {
  border-color: #409eff;
  background: rgba(64, 158, 255, 0.05);
}

.recharge-radio.is-checked::before {
  content: '';
  position: absolute;
  top: 0;
  right: 0;
  width: 0;
  height: 0;
  border-style: solid;
  border-width: 0 30px 30px 0;
  border-color: transparent #409eff transparent transparent;
}

.recharge-label {
  font-size: 22px;
  font-weight: 600;
  color: #2c3e50;
  display: block;
  margin-bottom: 10px;
}

.bonus-label {
  font-size: 16px;
  color: #67c23a;
  background: rgba(103, 194, 58, 0.1);
  padding: 6px 12px;
  border-radius: 6px;
  display: inline-block;
  margin-top: 8px;
}

.custom-amount-item {
  margin-top: 30px;
  width: 100%;
  max-width: 400px;
}

.custom-amount-input .el-input__wrapper {
  padding: 12px 20px;
  border-radius: 15px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  font-size: 18px;
  height: 100px; /* 设置高度为 100px */
}

.custom-amount-input :deep(.el-input__inner) {
  font-size: 18px;
  height: 100%; /* 内部输入框高度占满父容器 */
}

.amount-prefix {
  color: #409eff;
  font-size: 18px;
  font-weight: bold;
  margin-right: 8px;
}

.payment-container {
  width: 100%;
  max-width: 900px;
  margin-top: 30px;
}

.payment-title {
  font-size: 18px;
  color: #2c3e50;
  margin-bottom: 20px;
  text-align: center;
  font-weight: 500;
}

.payment-methods {
  .el-radio-group {
    display: flex;
    gap: 40px;
    justify-content: center;
  }

  .el-radio {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    width: 200px;
    height: 200px;
    padding: 20px;
    border: none;
    border-radius: 20px;
    transition: all 0.3s ease;
    aspect-ratio: 1;

    span {
      font-size: 22px;
      margin-top: 20px;
      margin-left: 0;
      font-weight: 500;
      text-align: center;
    }

    &:hover {
      transform: translateY(-2px);
      background: rgba(64, 158, 255, 0.05);
    }

    &.is-checked {
      background: rgba(64, 158, 255, 0.1);
    }

    :deep(.el-radio__input) {
      position: absolute;
      top: 15px;
      left: 15px;
    }
  }
}

.payment-icon {
  width: 60px;
  height: 60px;
  object-fit: contain;
}

.payment-info {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 10px;
  font-size: 16px;
  margin-top: 10px;
}

.payment-value {
  color: #409eff;
  font-weight: 500;
}

.button-container {
  margin-top: 40px;
  width: 100%;
  display: flex;
  justify-content: center;
}

.submit-button {
  width: 100%;
  max-width: 400px;
  padding: 20px 40px;
  font-size: 20px;
  font-weight: 600;
  border-radius: 12px;
  background: linear-gradient(135deg, #409eff, #67c2ff);
  border: none;
  color: white;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

.submit-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(64, 158, 255, 0.25);
  background: linear-gradient(135deg, #3a8ee6, #5ab6ff);
}

.submit-button:active {
  transform: translateY(0);
}

.submit-button::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(
    120deg,
    transparent,
    rgba(255, 255, 255, 0.2),
    transparent
  );
  transition: 0.5s;
}

.submit-button:hover::before {
  left: 100%;
}

.recharge-dialog {
  :deep(.el-dialog__header) {
    padding: 20px;
    margin: 0;
    text-align: center;
    border-bottom: 1px solid #eee;
  }

  :deep(.el-dialog__body) {
    padding: 30px;
  }
}

.dialog-content {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.amount-info, .bonus-info {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 10px;
  font-size: 16px;
}

.amount-value {
  font-size: 24px;
  color: #409eff;
  font-weight: bold;
}

.bonus-value {
  color: #67c23a;
  font-weight: bold;
}

.dialog-footer {
  display: flex;
  justify-content: center;
  gap: 20px;
  padding-top: 20px;
  border-top: 1px solid #eee;
}

@media (max-width: 768px) {
  .recharge-page {
    padding: 20px;
  }

  .recharge-radio-group {
    grid-template-columns: 1fr;
    gap: 20px;
  }

  .recharge-radio {
    padding: 25px;
  }

  .custom-amount-item {
    max-width: 100%;
    padding: 0 15px;
  }

  .payment-container {
    flex-direction: column;
    align-items: center;
    gap: 15px;
  }

  .payment-methods .el-radio-group {
    flex-direction: column;
    align-items: center;
    gap: 15px;
  }

  .payment-methods .el-radio {
    width: 100%;
    max-width: none;
  }
}
</style>